package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.pojo.Permission;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page page = permissionDao.selectByCondition(queryString);
        List<Permission> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,rows);
    }

    public void add(Permission permission) {
        if (permission!=null) {
            permissionDao.add(permission);
        }
    }

    public Permission findById(Integer id) {
        if (id>0) {
            return permissionDao.findById(id);
        }else {
            throw new RuntimeException("id值不正确");
        }
    }

    public void deleteById(Integer id) {
        permissionDao.delete(id);
    }


    public void edit(Permission permission) {
        if (permission!=null){
            permissionDao.edit(permission);
        }
    }

    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
